﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>收货地址</title>
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="yes" name="apple-touch-fullscreen">
    <meta name="format-detection" content="telephone=no">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
    <link href="../css/ydui.css?v=1.1.4" rel="stylesheet" />
    <script src="../js/ydui.flexible.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/all.css?v=20180302" />
    <style type="text/css">
        .m-cell {
            background: none;
        }

            .m-cell:after {
                border: none;
            }

        .cell-item {
            margin-top: .2rem;
            background: #fff;
            border-top: 1px solid #E7E7E7;
            border-bottom: 1px solid #e7e7e7;
        }
    </style>
</head>

<body ms-controller="addressview" class="ms-controller">
    <div class="g-flexview">
        <header class="m-navbar">
            <a onclick="history.back()" class="navbar-item"><i class="back-ico"></i></a>
            <div class="navbar-center"><span class="navbar-title">收货地址</span></div>
        </header>
        <section class="g-scrollview">
            <div class="m-cell">
                <div class="cell-item margin">
                    <div class="cell-left">联系人：</div>
                    <div class="cell-right"><input type="text" class="cell-input" placeholder="请填写收货人姓名" autocomplete="off" ms-duplex="@ModelData.Receiver" /></div>
                </div>
                <div class="cell-item margin">
                    <div class="cell-left">手机号：</div>
                    <div class="cell-right"><input type="number" class="cell-input" placeholder="请填写收货人手机号" autocomplete="off" ms-duplex="@ModelData.Telephone" oninput="if(value.length>11)value=value.slice(0,11)"/></div>
                </div>
                <div class="cell-item">
                    <div class="cell-left">配送至：</div>
                    <label class="cell-right cell-arrow">
                        <select class="cell-select" ms-duplex="@ModelData.SQID">
                            <option value="">请选择配送的社区</option>
                            <option ms-for="el in @ListSheQu" ms-attr="{value:el.ID}">{{el.Title}}</option>
                        </select>
                    </label>
                </div>
                <div class="cell-item" style="margin: 0;border-top:none ;">
                    <div class="cell-right">
                        <textarea class="cell-textarea" placeholder="详细地址(门牌号/楼层等)" ms-duplex="@ModelData.Address"></textarea>
                    </div>
                </div>
                <label class="cell-item">
                    <span class="cell-left">设为默认地址</span>
                    <label class="cell-right" >
                        <input type="checkbox" class="m-switch" ms-on-tap="@SetDefault()">
                    </label>
                </label>
            </div>
            <a class="btn  btn-primary" style="width:95%;margin:0.2rem;" ms-on-tap="@Save()">
                保存
            </a>
        </section>
        
    </div>
    <script src="//cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="../js/ydui.js"></script>
    <script src="../js/avalon.mobile.min.js"></script>
    <script src="../js/ComFunJS.js?v=20180331"></script>
    <script type="text/javascript">

        var model = avalon.define({
            $id: "addressview",
            ModelData: { "Receiver": "", "Telephone": "", "Address": "", "SQID": "", "IsDefault": 0 },
            ListSheQu: [],
            returnurl: ComFunJS.getQueryString("returnurl"),
            ID:ComFunJS.getQueryString("id"),
            get: function () {
                $.getJSON("/api/base/shequ/list", function (r) {
                    model.ListSheQu = r.result;

                })
                if (model.ID) {
                    $.getJSON("/api/user/address/model/" + model.ID, function (r) {
                        model.ModelData = r.result;
                    })
                }
                
            },
            SetDefault: function () {
                model.ModelData.IsDefault = model.ModelData.IsDefault == 1 ? 0 : 1;
            },
            Save: function () {
                $.post("/api/user/address/add", model.ModelData.$model, function (r) {
                    if (r.error == "") {
                        YDUI.dialog.toast("保存成功", "none", 2000, function () {
                            var url = "address.html";
                            if (model.returnurl) {
                                url += "?returnurl=" + model.returnurl;
                            }
                            window.location.replace(url);
                        });
                    } else {
                        YDUI.dialog.toast(r.error, "error", 2000);
                    }

                })

            }
        })
        avalon.ready(function () {
            if (ComFunJS.isLogin(true)) {
                model.get();
            }
           

        })

    </script>
</body>
</html>
